<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../css/api.css" />
		<link rel="stylesheet" type="text/css" href="../../css/style.css" />
		<link rel="stylesheet" type="text/css" href="../../css/btnplus.css" />
		<style type="text/css">
			.update_pwd .form li{ padding: 0 15px; position: relative;}
			.update_pwd .form li:not(:last-child)::after{position: absolute; content: ""; left: 0; right: 0; bottom: 0; border-bottom: 1px #d4d4d4 solid; transform: scaleY(0.5);}
			.update_pwd .form p{ font-size: 14px; color: #333; line-height: 50px;}
			.update_pwd .form input{ display:block; border: 0; font-size: 14px; color: #333; padding: 15px 0; width: 100%; background-color: transparent; border-radius: 0;}
			.update_pwd .form button{ display:block; border: 0; font-size: 14px; color: #3262F1; height: 50px; background-color: transparent; border-radius: 0;}
			.update_pwd .form span{ display:block; font-size: 12px; color: #999; line-height: 30px;}
			.update_pwd .btn{ padding: 35px 15px;}
			
			.display-show { display:inline; }
			.disable-btn { color:#ccc !important; }
		</style>
	</head>
	<body>
		<div class="update_pwd">
			<ul class="form">
				<li>
					<div class="m_cell_hd m_cell_primary"><p>手机号</p></div>
					<div class="m_cell_ft"><p id="mobile">--</p></div>
				</li>
				<li>
					<div class="m_cell_hd m_cell_primary"><input type="number" name="vcode" id="vcode" value="" placeholder="请输入验证码" /></div>
					<div class="m_cell_ft" id="senddiv"><button id="btnSendCode" tapmode onclick="sendMessage();">获取验证码</button></div>
				</li>
				<li>
				  <input type="password" name="password" id="password" value="" placeholder="请输入新密码" />
				</li>
				<li>
					<span>6-16位，数字及大小写字母</span>
				</li>
			</ul>
			<ul class="btn">
				<button class="d_btn" tapmode="btnpress" id="login-btn" onclick="setPwd();">确定</button>
			</ul>
		</div>
	</body>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script type="text/javascript" src="../../script/app.js"></script>
	<script type="text/javascript" src="../../script/md5.js"></script>
	<script type="text/javascript">
		var UIInput;
		var curPwdinputId = 0; //当前密码输入框ID
		var curPwdinputType = 'password'; //当前密码输入框类型

		var isrun = 0; //变量控制是否可以点击发短信按钮
		var InterValObj; //timer变量，控制时间
		var count = 60; //间隔函数，1秒执行
		var curCount;//当前剩余秒数
		var keyboardId = 0;//当前键盘ID
		var mobile;
		apiready = function() {
			api.parseTapmode();
			UIInput = api.require('UIInput');
			fnInitUIInput();
			api.getPrefs({
				key: 'user_info'
			}, function(ret, err) {
				if (ret && ret.value)
				{
					var userinfo = JSON.parse(ret.value);
					mobile = userinfo.mobile;
					$api.text($api.byId('mobile'),userinfo.mobile);
					
				}
			});
			
			api.addEventListener({
                name: 'navitembtn'
            }, function(ret, err) {
                if (ret.type == 'left') {
					api.closeWin({
					});
                }
            });
		}
		function sendMessage() {
			if(isrun == 1)
			{
				return false;
			}
			
			hideInput();//收起键盘
			//发送验证码
		  　curCount = count;
			
		　　//设置button效果，开始计时
			//$api.attr($api.byId('btnSendCode'),'disabled','true');
			$api.addCls($api.byId('btnSendCode'), 'disable-btn');
			$api.text($api.byId('btnSendCode'),"" + curCount + "s");
			InterValObj = JInterval(SetRemainTime,1000);
			isrun = 1;
			
			var paramsObj = {
				app_key: kAppKey,
				mobile: mobile,
				deviceId: api.deviceId,
				sendtype: 2
			}
			var _sign = getSign(paramsObj, kAppKey, kAppSecret);
			paramsObj.signature = _sign;
			api.ajax({
				url: ApiUrl + '/v1/token/sendsms',
				method: 'post',
				data: {
					values: paramsObj
				}
			}, function(ret, err) {
				if (ret) {
					toast('验证码已发送');
				} else {
					toast(err.body.message);
					console.log(JSON.stringify(err));
				}
			});
			
		}
		function JInterval(funcName,time){
			   var args=[];
			   for(var i=2;i<arguments.length;i++){
				   args.push(arguments[i]);
			   }
			return window.setInterval(function(){
			 funcName.apply(this,args);
			},time);
		}
		   
		//timer处理函数
		function SetRemainTime() {
			if (curCount == 0) {                
				isrun = 0;
				window.clearInterval(InterValObj);//停止计时器
				//$api.removeAttr($api.byId('btnSendCode'), 'disabled')
				$api.removeCls($api.byId('btnSendCode'), 'disable-btn');
				$api.text($api.byId('btnSendCode'),"重发验证码");
			}
			else {
				curCount--;
				$api.text($api.byId('btnSendCode'),"" + curCount + "s");
			}
		}
		function hideInput(){
			//隐藏键盘
			UIInput.closeKeyboard({
				id: keyboardId
			});
		}
		function fnInitUIInput(){
			//计算位置
			openUIinput('vcode','请输入验证码','number','text',8);
			openUIinput('password','请输入新密码','default','password','',0);
		}
		function openUIinput(domid,placeholder_str,keyboardType,inputType,pwdval,maxnum){
			//打开UIinput
			var password = $api.byId(domid);
			var password_rect = $api.offset(password);
			UIInput.open({
				rect: {
					x: password_rect.l,
					y: password_rect.t+1,
					w: password_rect.w,
					h: password_rect.h -2
				},
				styles: {
					bgColor: '#fff',
					size: 14,
					color: '#333',
					placeholder: {
						color: '#D4D4D4'
					}
				},
				autoFocus: false,
				maxRows: 1,
				placeholder: placeholder_str,
				keyboardType: keyboardType,
				inputType: inputType,
				fixedOn: api.frameName,
				fixed: false
			}, function(ret) {
				
				if (ret.eventType == 'change') {
					keyboardId = ret.id;
					UIInput.value({
							id: ret.id,
							//msg : '设置输入框的值'
					}, function(ret, err) {
						if (ret) {
							var _msg = ret.msg;
							if(maxnum > 0) {
								var _msglen = _msg.length;
								if(_msglen > maxnum)
								{
									_msglen = maxnum;
									_msg = ret.msg.substr(0,maxnum);
									UIInput.value({
										id: keyboardId,
										msg: _msg
									});
								}
							}
							$api.val(password,_msg);
						} else {
							//alert(JSON.stringify(err));
						}
					});
				} else if (ret.eventType == 'show' && domid == 'password') {
					curPwdinputId = ret.id; //获取验证码输入框ID
					if(pwdval!='')
					{
						UIInput.value({
							id: curPwdinputId,
							msg: pwdval
						});
					}
				} else {
					//alert(JSON.stringify(ret));
				}
			});
			
		}
		function setPwd() {
			hideInput();//收起键盘
			var vcode = $api.val($api.byId('vcode')).trim();
			var password = $api.val($api.byId('password')).trim();
			
			if($api.cssVal($api.byId('login-btn'),'opacity')!=1)
			{
				return false;
			}
			if(vcode=='')
			{
				toast("请输入验证码");
				return false;
			}
			if(password.length < 6 || password.length > 16)
			{
				toast("请输入6-16位密码");
				return false;
			}
			if(!checknumletter(password)) {
				toast("密码只能为数字和大小写字母");
				return false;
			}
			showbtnloading('login-btn');
			api.getPrefs({
				key: 'user_info'
			}, function(ret, err) {
				if (ret && ret.value)
				{
					var userinfo = JSON.parse(ret.value);
					console.log(kAppKey+';'+userinfo.access_token+';'+userinfo.uid);
					api.ajax({
						url: ApiUrl + '/v1/userinfo',
						method: 'post',
						cache: false,
						timeout: 10,
						headers: {
							authorization: kAppKey+';'+userinfo.access_token+';'+userinfo.uid
						},
						data: {
							values: {
								type: 'setpwd',
								vcode: vcode,
								password: password
							}
						}
					}, function(ret, err) {
						if (ret) {
							api.alert({
								title: '修改密码',
								msg: '密码修改成功！请使用新密码重新登录!',
							}, function(ret, err) {
								if(ret.buttonIndex == 1)
								{
									setTimeout(function() {
										api.closeWin({
										});
									}, 1500);
									api.removePrefs({
										key: 'user_info'
									});
									var userjson = {};
									var usersetting = 'updateuser('+ JSON.stringify(userjson) +');';
									setTimeout(function(){
										openNavWin('#fff','login_win','../login/login_frm.html','',true,false,false,[],{});
									}, 0);
								}
							});
						
							
						} else {
							console.log(JSON.stringify(err));
							toast(err.body.message);
						}
						hidebtnloading('login-btn','确认');
					});
					}
			});
		}
	</script>
</html>